<?php

require('memcahe.class.php');

class Db
{
	private $host = DBHOST;
	private $root = DBROOT;
	private $password = DBPASS;
	private $dbname = DBNAME;
	public $tbname;

	public function __construct()
	{
		$this->conn = mysql_connect($this->host, $this->root, $this->password) or die("Could not connect: " . mysql_error());
		mysql_select_db($this->dbname, $this->conn); 
		mysql_query("set names 'utf8'");
	}

	public function __get($index)
	{
		if($this->$index)
		{
			return $this->$index;
		}else{
			return null;
		}		
	}

	public function __call($action, $arr) //使用__call尝试调用一个不存在/不可用的成员方法
    {
		$this->condition($action, $arr);
		return $this->dosql($action);
    }
	
	function dosql($action)
	{
		$q = mysql_query($this->sql);
		//print_r($this->sql);exit;
		switch($action)
		{
			case '_select':
				if($q):
					while($row = mysql_fetch_assoc($q)) 
					{
					   $return[] = $row;
					}
				endif;
				/*
				if(count($return) == 1)
				{
					$return = $return[0];
				}*/
				/*
				$memcahekey = md5($this->sql);
				if($this->get($memcahekey))
				{
					$return = $this->get($memcahekey);
				}else{
					while($row = mysql_fetch_assoc($q)) 
					{
					   $return[] = $row;
					}
					if(count($return) == 1)
					{
						$return = $return[0];
					}
					$this->add(array($memcahekey=>$return));
				}
				*/
				return $return;
				break;
			default:
				return $q;
				break;
		}
		 
	}

	function condition($action, $arr)
	{
		$data = $arr[0];
		$item = $arr[1];
		switch($action)
		{
			case '_insert':
				$index_string = implode(', ', array_keys($data));
				$value_string = '"'.implode('", "', $this->doString(array_values($data))).'"';
				$this->sql = "insert into ".$this->tbname."(".$index_string.") values (".$value_string.")";
				break;
			
			case '_update':
				if(is_array($data))
				{
					$tag = 0;
					foreach($data as $key => $val)
					{
						if($tag == 0)
						{
							$string .= $key." = '".$this->doString($val)."'";
						}else{
							$string .= ', '.$key." = '".$this->doString($val)."'";
						}
						$tag++;
					}
				}

				if(is_array($item))
				{
					$tag = 0;
					foreach($item as $key => $val)
					{
						switch(trim(end(explode(" ",$key))))
						{
							case 'IN':
								$string .= ($tag?' and ':' where ').$key."('".$val."')";
								break;
							default:
								$string .= ($tag?' and ':' where ').$key."= '".$val."'";
								break;
						}
						$tag++;
					}
				}
				$this->sql = "update ".$this->tbname." set ".$string;
				break;

			case '_select':				
				if(is_array($item))
				{
					$tag = 0;
					$string = "";
					foreach($item as $key => $val)
					{						
						switch(trim(end(explode(" ",$key))))
						{
							case 'IN':
								$string .= ($tag?' and ':' where ').$key."('".$val."')";
								break;
							case 'LIKE':
								if(is_array($val))
								{
									$string .= $tag?' and ':' where ';
									
									foreach($val as $k=>$v)
									{
										$string .= $k==0?$key." '%".$v."%'":" or ".$key." '%".$v."%'";										
									}
								}elseif($val){
									$string .= ($tag?' and ':' where ').(trim($val)?$key." '%".$val."%'":"");
								}
								break;
							case 'LIMIT':
								$string .= ' limit '.$val;
								break;
							case 'GROUP':
								$string .= ' group by '.$val;
								break;
							case 'DESC':
							case 'ASC':
								$string .= ' order by '.$val." ".$key;
								break;
							default:
								if($val)
								{
									$string .= ($tag?' and ':' where ').$key."= '".$val."'";
								}
								break;
						}
						$tag++;
					}
				}
				$getData = is_array($data)?implode(',',$data):($data ? $data : '*');
				$this->sql = "select ".$getData." from ".$this->tbname.$string;
				break;
			case '_delete':
				if(is_array($item))
				{
					$tag = 0;$string="";
					foreach($item as $key => $val)
					{
						switch(trim(end(explode(" ",$key))))
						{
							case 'IN':
								$string .= ($tag?' and ':' where ').$key."('".$val."')";
								break;
							default:
								$string .= ($tag?' and ':' where ').$key."= '".$val."'";
								break;
						}
						$tag++;
					}
				}
				//$getData = is_array($data)?implode(',',$data):($data ? $data : '');
				$this->sql = "DELETE FROM ".$this->tbname.$string;
				break;
			default:
				die('Erro：你访问的方法不存在！');
				break;
		}
	}

	function doString($string)
	{
		$string = str_replace(array('\"',"\'"), array('"',"'"),$string);
		$string = str_replace(array(PHP_EOL,'"',"'"), array('','\"',"\'"),$string);
		return preg_replace('/>\s+</', '><', $string);
	}
}